package com.yt.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yt.project.model.dto.douyin.DouyinQueryRequest;
import com.yt.project.model.entity.Bilibili;
import com.yt.project.model.entity.Douyin;
import com.yt.project.model.entity.Kuaishou;
import com.yt.project.model.vo.DescVO;
import com.yt.project.model.vo.DouyinVO;
import com.yt.project.model.vo.TagDistVO;
import com.yt.project.service.DouyinService;
import com.yt.project.mapper.DouyinMapper;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;
import java.util.stream.Collectors;

/**
* @author lenovo
* @description 针对表【douyin】的数据库操作Service实现
* @createDate 2023-04-19 21:25:23
*/
@Service
public class DouyinServiceImpl extends ServiceImpl<DouyinMapper, Douyin>
    implements DouyinService{

    @Override
    public Page<DouyinVO> pageDouyin(DouyinQueryRequest douyinQueryRequest) {

        long current = 1;
        long pageSize = 10;
        if (douyinQueryRequest != null) {
            current = douyinQueryRequest.getCurrent();
            pageSize = douyinQueryRequest.getPageSize();
        }
        if (douyinQueryRequest == null) {
            douyinQueryRequest = new DouyinQueryRequest();
        }
        System.err.println(douyinQueryRequest.getSearchText());
        QueryWrapper<Douyin> douyinQueryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(douyinQueryRequest.getSearchText())) {
            douyinQueryWrapper.like("nickname", douyinQueryRequest.getSearchText());
        }

        if (douyinQueryRequest.getShare() != null) {
            douyinQueryWrapper.gt("share", douyinQueryRequest.getShare());
        }
        if (douyinQueryRequest.getComment() != null) {
            douyinQueryWrapper.gt("comment", douyinQueryRequest.getComment());
        }
        if (douyinQueryRequest.getZan() != null) {
            douyinQueryWrapper.gt("zan", douyinQueryRequest.getZan());
        }
        if (douyinQueryRequest.getFan() != null) {
            douyinQueryWrapper.gt("sum_fan", douyinQueryRequest.getFan());
        }
        if (douyinQueryRequest.getTypes() != null && douyinQueryRequest.getTypes().size() > 0) {
            douyinQueryWrapper.in("ttype", douyinQueryRequest.getTypes());
        }

        if (douyinQueryRequest.getStartDate() != null && douyinQueryRequest.getEndDate() != null) {
            // 数据库startTime > bilibiliQueryRequest.getStartDate()  && 数据库endDate < bilbxxxx
            douyinQueryWrapper.gt("startTime", douyinQueryRequest.getStartDate());
            douyinQueryWrapper.lt("endTime", douyinQueryRequest.getEndDate());
        }

        Page<Douyin> douyinPage = this.page(new Page<>(current, pageSize), douyinQueryWrapper);
        List<Douyin> douyins = douyinPage.getRecords();
        List<DouyinVO> bilibiliVOS = douyins.stream().map(douyin -> {
            DouyinVO douyinVO = new DouyinVO();
            douyinVO.setId(douyin.getId());
            douyinVO.setNickname(douyin.getNickname());
            douyinVO.setNewPost(douyin.getNewPost());
            douyinVO.setShare(douyin.getShare());
            douyinVO.setComment(douyin.getComment());
            douyinVO.setZan(douyin.getZan());
            douyinVO.setFan(douyin.getSumFan());
            String type = douyin.getTtype();
            List<String> types = new ArrayList<>();
            types.add(type);
            douyinVO.setTypes(types);
            douyinVO.setAvatar(douyin.getAvatarUrl());
            return douyinVO;
        }).collect(Collectors.toList());
        Page<DouyinVO> douyinVOPage = new Page<>();
        douyinVOPage.setRecords(bilibiliVOS);
        douyinVOPage.setTotal(douyinPage.getTotal());
        return douyinVOPage;
    }

    @Override
    public DescVO getDescById(Integer id) {
        QueryWrapper<Douyin> queryWrapper = new QueryWrapper<>();
        Douyin douyin = this.getById(id);
        String nickname = douyin.getNickname();
        QueryWrapper<Douyin> douyinQueryWrapper = new QueryWrapper<>();
        douyinQueryWrapper.eq("nickname", nickname);
        douyinQueryWrapper.orderByAsc("start_date");
        List<Douyin> douyinList = this.list(douyinQueryWrapper);
        // 计算权重：
        List<String> dates = new ArrayList<>();
        List<Integer> datas = new ArrayList<>();
        douyinList.forEach(d -> {
            // 0.2
            Long share = d.getShare();
            // 0.2
            Long comment = d.getComment();
            Long newFan = d.getFan();
            Long fan = d.getSumFan();
            Long zan = d.getZan();
            // 计算权重
            Double data = 0.2 * share + 0.2 * comment + 0.2 * fan + 0.2 * newFan + 0.2 * zan;
            Date startDate = d.getStartDate();
            Date endDate = d.getEndDate();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy年MM月dd日");
            String date = formatter.format(startDate) + "-\n" + formatter.format(endDate);
            dates.add(date);
            datas.add(data.intValue() / 100);
        });
        DescVO descVO = new DescVO();
        descVO.setFan(String.valueOf(douyin.getSumFan() > 10000 ? (douyin.getSumFan() / 10000) + "w" : douyin.getFan()));
        descVO.setComment(String.valueOf(douyin.getComment() > 10000 ? (douyin.getComment() / 10000) + "w" : douyin.getComment()));
        descVO.setShare(String.valueOf(douyin.getShare() > 10000 ? (douyin.getShare() / 10000) + "w" : douyin.getShare()));
        descVO.setId(douyin.getId());
        descVO.setNickname(douyin.getNickname());
        descVO.setAvatarUrl(douyin.getAvatarUrl());
        descVO.setDate(dates);
        descVO.setData(datas);
        return descVO;
    }

    @Override
    public List<TagDistVO> getTagsDist(Integer id) {
        QueryWrapper<Douyin> douyinQueryWrapper = new QueryWrapper<>();
        douyinQueryWrapper.select("ttype");
        List<Douyin> douyinList = this.list(douyinQueryWrapper);
        List<String> tagList = douyinList.stream().map(Douyin::getTtype).collect(Collectors.toList());
        //System.out.println(tagList);
        Map<String, Long> map = new HashMap<>();
        for (String tag : tagList) {
            if (map.containsKey(tag)) {
                map.put(tag, map.get(tag) + 1);
            } else {
                map.put(tag, 1L);
            }
        }
        List<TagDistVO> tagDistVOList = new ArrayList<>();
        map.forEach((k, v) -> {
            TagDistVO tagDistVO = new TagDistVO();
            tagDistVO.setName(k);
            tagDistVO.setValue(v);
            tagDistVOList.add(tagDistVO);
        });
        return tagDistVOList;
    }
}